using UnityEngine;
using System.Collections;
using System.Collections.Generic;

[ExecuteInEditMode()]
[AddComponentMenu("GK/Sequencer/EditorView/gkSequencerActionEditorView")]
[RequireComponent(typeof(gkGUIBoxRenderer))]
[RequireComponent(typeof(gkScreenDraggingController))]
[RequireComponent(typeof(gkSequencerEditorViewTicker))]
/// \class  gkSequencerActionEditorView
/// \brief 	The action view
public class gkSequencerActionEditorView : MonoBehaviour
{
#if UNITY_EDITOR
	/// The action
	gkSequencerAction m_rAction;
	
	/// The GUI box renderer
	gkGUIBoxRenderer m_rGUIBoxRenderer;
	
	/// The screen transform
	gkScreenTransform m_rScreenTransform;
	
	/// \brief  Called after script loading
	void Start()
	{
		// Grab the action
		m_rAction = GetComponent<gkSequencerAction>();
		
		// Grab the GUI box renderer
		m_rGUIBoxRenderer = GetComponent<gkGUIBoxRenderer>();
		m_rGUIBoxRenderer.styleName = "Action_Clip";
		
		// Grab the screen transform
		m_rScreenTransform = GetComponent<gkScreenTransform>();
		
		// Conform the gui to the action
		ActionToGUI();
	}
	
	/// \brief  Called on sequencer GUI update
	public void DisplaySequencerGUI()
	{
		if(m_rAction != null)
		{
			// Display the clip GUI
			SendMessage("OnGUIRender", SendMessageOptions.DontRequireReceiver);
			
			// Conform the action to the GUI
			GUIToAction();
		}
	}
	
	/// \brief  Make the action parameters conform to his GUI representation   
	void GUIToAction()
	{
		Rect rectangleBounds;
		
		// Get the rectangleBounds
		rectangleBounds = m_rScreenTransform.rectangleBounds;
		
		// Set the action start and end time
		m_rAction.startTime = rectangleBounds.xMin;
		m_rAction.endTime = rectangleBounds.xMax;
	}
	
	/// \brief  Make the action parameters conform to his GUI representation   
	void ActionToGUI()
	{
		Rect rectangleBounds;
		
		// Compute the rectangleBounds
		rectangleBounds = new Rect(m_rAction.startTime, 7.5f,  m_rAction.endTime - m_rAction.startTime, 15.0f);
				
		// Set the rectangle bounds
		m_rScreenTransform.rectangleBounds = rectangleBounds;
	}
#endif
}